package com.lyl.domaindemo.resource.user;

import com.lyl.domaindemo.Infrastructure.CommonResults;
import com.lyl.domaindemo.Infrastructure.Constants;
import com.lyl.domaindemo.application.user.UserUseCase;
import com.lyl.domaindemo.domain.user.User;
import com.lyl.domaindemo.resource.user.dto.request.UserRequest;
import com.lyl.domaindemo.resource.user.dto.response.UserResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lyl
 * @date 2023/5/17
 */

@Slf4j
@RestController
public class UserResource {

    private final UserUseCase userUseCase;

    public UserResource(UserUseCase userUseCase) {
        this.userUseCase = userUseCase;
    }

    @GetMapping("/userList")
    public CommonResults<UserResponse> getUsers(Pageable pageable){
        Page<User> users = userUseCase.getUsers(pageable);
        return new CommonResults<>("200", Constants.SUCCESS, users.map(UserResponse::of));
    }

    @PostMapping("/insertUser")
    public CommonResults insertUser(@RequestBody UserRequest userRequest){
        userUseCase.saveUser(userRequest);
        return CommonResults.success();
    }

    @PatchMapping("/updateUser")
    public CommonResults updateUser(@RequestBody UserRequest userRequest){
        userUseCase.saveUser(userRequest);
        return CommonResults.success();
    }

    @DeleteMapping("/deleteUser")
    public CommonResults deleteUser(@RequestParam("ids") List<Long> ids){
        userUseCase.deleteUser(ids);
        return CommonResults.success();
    }
}
